package com.example.product_practices.controller.login;

import com.alibaba.fastjson.JSON;
import com.example.product_practices.constant.AuthServerConstant;
import com.example.product_practices.domain.User;
import com.example.product_practices.exception.BizCodeEnume;
import com.example.product_practices.vo.SocialUser;
import com.example.product_practices.service.IUserService;
import com.example.product_practices.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xin.altitude.cms.common.entity.AjaxResult;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Slf4j
@Controller
@CrossOrigin
public class OAuth2Controller {
    @Autowired
    IUserService userService;
//    https://gitee.com/oauth/authorize?client_id=41a819c96a853feea52776db53b512215d0b9329d24e2db7d20e6e6721e77a2e&redirect_uri=http://localhost:8080/oauth2.0/gitee/success&response_type=code
    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code")String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        HashMap<String,String> map = new HashMap<>();
        map.put("grant_type","authorization_code");
        map.put("code",code);
        map.put("client_id","41a819c96a853feea52776db53b512215d0b9329d24e2db7d20e6e6721e77a2e");
        //TODO 应用回调地址
        map.put("redirect_uri","http://localhost:8080/oauth2.0/gitee/success");//
        map.put("client_secret","5546593c48b4e906bbf56f236a4e9b6d149c3bfbf907119d99adfe7d97c54fe9");
        HttpResponse post = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), map, new HashMap<>());
        System.out.println("post.getStatusLine().getStatusCode() = " + post.getStatusLine().getStatusCode());
        if (post.getStatusLine().getStatusCode()==200){
            String string = EntityUtils.toString(post.getEntity());
            SocialUser socialUser = JSON.parseObject(string, SocialUser.class);
            //知道了哪个社交用户
            //1）、当前用户如果是第一次进网站，自动注册进来（为当前社交用户生成一个会员信息，以后这个社交账号就对应指定的会员）
            //登录或者注册这个社交用户
            System.out.println(socialUser.getAccess_token());
            //调用远程服务
            //TODO 远程调用(需要改为本地)
                User data = userService.oauthlogin(socialUser);
            if (data != null) {
                log.info("登陆成功{}",data.toString());
                //                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                return "redirect:http://localhost:8080/doc.html#/home";
            }else {
                throw new Exception(BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
            }

        }else {
            //TODO 登录页
            return "redirect:http://localhost:8080/doc.html#/home";
        }
    }
}
